///////////////////////////////////////////////////
// Election Administration Polarization
// Make RD Analaysis Dataset
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"

* Bring in the presidential election analysis data
use "$path/analysis_data/rd_election_analysis_data.dta", clear
keep if office=="pres"

* Run the bandwidth selection code for the simplest version of the outcome
rdbwselect vs_dem rv, vce(cluster election_id) kernel(uniform)
gl bw = e(h_mserd)

* Spec 1, Simple Outcome
reg vs_dem treat rv treat_rv if abs(rv)<$bw, vce(clust election_id)
rdtab vs_dem treat rv election_id, est_command("reg") new("B") tau(0.02) alpha(0.05) onesided

* Spec 2, Simple Outcome
reg vs_dem treat rv treat_rv if abs(rv)<$bw*2, vce(clust election_id)
rdtab vs_dem treat rv election_id, est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 3, Simple Outcome
reg vs_dem treat rv treat_rv if abs(rv)<$bw*0.5, vce(clust election_id)
rdtab vs_dem treat rv election_id, est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 4, Simple Outcome
rdrobust vs_dem rv, vce(cluster election_id) all
rdtab vs_dem treat rv election_id, est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided


* Run the bandwidth selection code for the simplest version of the outcome with the lag as a covariate
rdbwselect vs_dem rv, vce(cluster election_id) kernel(uniform) covs(lag_vs_dem)
gl bw = e(h_mserd)

* Spec 1, Simple Outcome with Lag
reg vs_dem treat rv treat_rv lag_vs_dem if abs(rv)<$bw, vce(clust election_id)
rdtab vs_dem treat rv election_id, est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 2, Simple Outcome with Lag
reg vs_dem treat rv treat_rv lag_vs_dem if abs(rv)<$bw*2, vce(clust election_id)
rdtab vs_dem treat rv election_id, est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 3, Simple Outcome with Lag
reg vs_dem treat rv treat_rv lag_vs_dem if abs(rv)<$bw*0.5, vce(clust election_id)
rdtab vs_dem treat rv election_id, est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 4, Simple Outcome with Lag
rdrobust vs_dem rv, vce(cluster election_id) all covs(lag_vs_dem)
rdtab vs_dem treat rv election_id if lag_vs_dem!=., est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided

* Make the table
qui {
cap log close
set linesize 255
log using "$path/output/rd_table_elec_simple.tex", text replace
noi di "\begin{table}[ht]"
noi di "\centering"
noi di "\caption{\textbf{Effect of Democratic Election Officials on Democratic Presidential Vote Share.}"
noi di "\label{tab:rd_table_elec_simple}}"
noi di "\begin{tabular}{lcccccccc}"
noi di "\toprule \toprule"
noi di " & \multicolumn{8}{c}{Dem Pres Vote Share}\\"
noi di " & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\"
noi di "\midrule"
noi di "Dem Elec Official & " %4.3f B[1,1] " & " %4.3f B[1,2] " & " %4.3f B[1,3] " & " %4.3f B[1,4] " & "
noi di " " %4.3f B[1,5] " & " %4.3f B[1,6] " & " %4.3f B[1,7] " & " %4.3f B[1,8] "\\"
noi di " & (" %4.3f B[2,1] ") & (" %4.3f B[2,2] ") & (" %4.3f B[2,3] ") & (" %4.3f B[2,4] ") & "
noi di " (" %4.3f B[2,5] ") & (" %4.3f B[2,6] ") & (" %4.3f B[2,7] ") & (" %4.3f B[2,8] ") \\[2mm]"
noi di "\midrule"
noi di "N & " %4.0f B[3,1] " & " %4.0f B[3,2] " & " %4.0f B[3,3] " & " %4.0f B[3,4] " "
noi di 		" & " %4.0f B[3,5] " & " %4.0f B[3,6] " & " %4.0f B[3,7] " & " %4.0f B[3,8] " \\ "
noi di "Clusters & " %4.0f B[4,1] " & " %4.0f B[4,2] " & " %4.0f B[4,3] " & " %4.0f B[4,4] " "
noi di 		" & " %4.0f B[4,5] " & " %4.0f B[4,6] " & " %4.0f B[4,7] " & " %4.0f B[4,8] " \\ "
noi di "Bandwidth & " %4.2f B[5,1] " & " %4.2f B[5,2] " & " %4.2f B[5,3] " & " %4.2f B[5,4] " "
noi di 	" & " %4.2f B[5,5] " & " %4.2f B[5,6] " & " %4.2f B[5,7] " & " %4.2f B[5,8] " \\ "
noi di "BW Selection & CCT & CCT*2 & CCT/2 & CCT & CCT & CCT*2 & CCT/2 & CCT \\"
noi di "Kernel & Unif & Unif & Unif & Tri & Unif & Unif & Unif & Tri \\"
noi di "Lagged Vote Share & No & No & No & No & Yes & Yes & Yes & Yes \\"
noi di "Min Detectable Effect  & " %4.3f B[7,1] " & " %4.3f B[7,2] " & " %4.3f B[7,3] " & " %4.3f B[7,4] " & "
noi di " " %4.3f B[7,5] " & " %4.3f B[7,6] " & " %4.3f B[7,7] " & " %4.3f B[7,8] " \\ "
noi di "\bottomrule \bottomrule"
noi di "\multicolumn{9}{p{1.05\textwidth}}{\footnotesize Robust standard errors in parentheses. "
noi di "The bandwidth row reports the number of maximum clerk win margin allowed for inclusion in each specificaiton. "
noi di "CCT refers to \cite{calonico2014robust} bandwidth selection procedure."
noi di "Min detectable effect refers to the minimum effect that a one-sided test with a 0.05 alpha would have 80\% power to detect. "
noi di "Lagged vote share captures whether lagged Democratic presidential vote share is included as a covariate in the regression. "
noi di "Unif means the specification uses a uniform kernel. Tri means the specification uses a triangular kernel.}"
noi di "\end{tabular}"
noi di "\end{table}"
log off
}

